package com.llg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llg.mapper.ProcessorSpecMapper;
import com.llg.model.code.CommonCode;
import com.llg.model.code.PageCode;
import com.llg.model.response.DataResult;
import com.llg.model.response.PageResult;
import com.llg.model.response.ResponseResult;
import com.llg.pojo.ProcessorSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ProcessorSpecService {
    @Autowired
    private ProcessorSpecMapper mapper;

    //通过品牌id查询全部处理器
    public PageResult<ProcessorSpec> findByBid(int bid){
        Example example = new Example(ProcessorSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bid", bid);
        List<ProcessorSpec> list = mapper.selectByExample(example);
        return new PageResult<>(CommonCode.SUCCESS, list.size(), 1, list);
    }

    //搜索并分页查询
    public PageResult<ProcessorSpec> findList(int currentPage,int pageSize,ProcessorSpec processorSpec){
        //参数判断
        if(currentPage <= 0) return new PageResult<>(PageCode.PAGE_ERR1, 0, 0, null);
        if(pageSize <= 0) return new PageResult<>(PageCode.SIZE_ERR, 0, 0, null);
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(ProcessorSpec.class);
        Example.Criteria criteria = example.createCriteria();
        if (processorSpec != null) {
            if (processorSpec.getName() != null && !processorSpec.getName().equals("")) {
                criteria.andLike("name", "%" + processorSpec.getName() + "%");
            }
        }
        List<ProcessorSpec> list = mapper.selectByExample(example);
        PageInfo<ProcessorSpec> page = new PageInfo<>(list);
        return new PageResult<>(CommonCode.SUCCESS, (int)page.getTotal(), page.getPages(), page.getList());
    }

    //通过id查
    public DataResult<ProcessorSpec> findById(int id){
        return new DataResult<>(CommonCode.SUCCESS, mapper.selectByPrimaryKey(id));
    }

    //增
    public DataResult<ProcessorSpec> add(ProcessorSpec processorSpec){
        mapper.insertSelective(processorSpec);
        return new DataResult<>(CommonCode.SUCCESS, processorSpec);
    }
    //删
    public ResponseResult delete(int id){
        mapper.deleteByPrimaryKey(id);
        return new ResponseResult(CommonCode.SUCCESS);

    }

    //改
    public ResponseResult update(ProcessorSpec processorSpec){
        mapper.updateByPrimaryKeySelective(processorSpec);
        return new ResponseResult(CommonCode.SUCCESS);
    }

}
